<template>
    <div class="register-container">
      <div class="register-form">
        <h2 style="color: aliceblue;">注册</h2>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password" show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="form.confirmPassword" show-password placeholder="请确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')">立即注册</el-button>
          </el-form-item>
        </el-form>
        <p class="login-link">
          <el-checkbox v-model="isAgreed" style="margin-top: 10px;">
            同意
          </el-checkbox>
          已有账号?
          <router-link to="/DengLu" :class="{ disabled: !isAgreed }" @click.prevent="handleRegisterClick">
            立即登录
          </router-link>
        </p>
      </div>
    </div>
  </template>
  <script>
  import axios from 'axios';
  export default {
    data() {
      const validateConfirmPassword = (rule, value, callback) => {
        if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          username: '',
          email: '',
          password: '',
          confirmPassword: '',
          isAgreed: false,
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '请确认密码', trigger: 'blur' },
            { validator: validateConfirmPassword, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      handleRegisterClick() {
        if (this.isAgreed) {
          this.$router.push('/DengLu');
        } else {
          alert('请先同意条款');
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.registerUser();
          } else {
            console.log('表单验证失败！');
            return false;
          }
        });
      },
      async registerUser() {
        try {
          const response = await axios.post('http://localhost:8090/resgisty', {
            username: this.form.username,
            email: this.form.email,
            password: this.form.password
          });
          if (response.status === 200) {
            alert('注册成功！');
            this.$router.push('/DengLu');
          } else {
            alert('注册失败，请重试。');
          }
        } catch (error) {
          console.error('注册失败:', error);
          if (error.response &&error.response.data) {
            alert(error.response.data.message || '注册失败，请重试。');
          } else {
            alert('注册失败，请重试。');
          }
        }
      }
    }
  };
  </script>
  
  <style scoped>
  .register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 93.5vh;
  }
  
  .register-form {
    background: rgba(144, 97, 118, 0.167);
    padding: 20px;
    border-radius: 9px;
    box-shadow: 0 5px 7px rgba(246, 247, 247, 0.852);
    width: 300px;
  }
  
  h2 {
    text-align: center;
    margin-bottom: 20px;
    color: #333;
  }
  
  .login-link {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
    color: #666;
  }
  
  .login-link a {
    color: #409eff;
    text-decoration: none;
  }
  
  .login-link a:hover {
    text-decoration: underline;
  }
  </style>
  